<?php
/**
 * User: hjd-yao
 * Date: 2018/11/5
 * Time: 17:15
 */

namespace App\Http\Middleware\Backend;


use App\Models\ManageAdminModel;
use Closure;
use Illuminate\Http\Request;

class CheckAuth extends Middleware
{
    public function handle(Request $request, Closure $next)
    {
        $action = $request->route()->getName();
        if(!$this->checkActions($action)){
            return redirect()->route(ManageAdminModel::init()->getJumpUrl());
        }

        return $next($request);
    }

    private function checkActions($route_name)
    {
        $mgr_id = ManageAdminModel::init()->getLoginId();
        if(ManageAdminModel::init()->isSuperAdmin($mgr_id)){
            return true;
        }
        $my_actions = ManageAdminModel::init()->getLoginAuth();
        if($my_actions&&in_array($route_name,$my_actions)){
            return true;
        }
        return false;
    }

}